package com.example.music

import android.Manifest
import com.hyjy.music.util.PermissionHelper.checkPermission
import android.os.Bundle
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Environment
import android.view.LayoutInflater
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.view.animation.AnimationUtils
import androidx.lifecycle.lifecycleScope
import com.example.music.databinding.ActivityWelcomeBinding
import com.example.music.shape.ShapeManager
import com.example.music.strategy.CalculateHandler
import com.hyjy.music.base.BaseActivity
import com.hyjy.music.util.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject

/**
 * 欢迎页
 */
class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
    companion object {
        private val PERMISSION_ARRAY = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )
        private const val PERMISSIONS_REQUEST_CODE = 1
    }

    private val shapeManager by inject<ShapeManager>()

    override fun initViewBinding(layoutInflater: LayoutInflater): ActivityWelcomeBinding {
        return ActivityWelcomeBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        AnimationUtils.loadAnimation(this, com.hyjy.music.R.anim.set1).apply {
            binding?.iv1?.startAnimation(this)
            setAnimationListener(object : AnimationListener {
                override fun onAnimationStart(animation: Animation?) {}
                override fun onAnimationRepeat(animation: Animation?) {}
                override fun onAnimationEnd(animation: Animation?) {
                    checkPermission(
                        this@WelcomeActivity,
                        PERMISSION_ARRAY,
                        PERMISSIONS_REQUEST_CODE
                    ) {
                        lifecycleScope.launch(Dispatchers.IO) {
                            val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).canonicalPath
                            copyFileToPhone(savePath = path) {
                                skipToMainActivity()
                            }
                        }
                    }
                }
            })
        }
    }

    private fun doSomething() {
        val a = CalculateHandler.doCalculate("+", 10, 3)
        val b = CalculateHandler.doCalculate("-", 10, 3)
        val c = CalculateHandler.doCalculate("*", 10, 3)
        println("a = $a")
        println("b = $b")
        println("c = $c")

        lifecycleScope.launch(Dispatchers.IO) {
            xmlToMap(this@WelcomeActivity, "config/hero_sui_tang.xml").forEach {
                val key = it.key
                val value = it.value
                println("${key}=${value}")
            }
            println("------")

            shapeManager.getArea()
            println("------")

            jsonToEntity<Map<String, String>>(
                this@WelcomeActivity,
                "config/hero_sui_tang.json"
            ).forEach {
                val key = it.key
                val value = it.value
                println("$key:$value")
            }
            println("------")
        }
    }

    private fun skipToMainActivity() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == PERMISSIONS_REQUEST_CODE) {
            var isGranted = true
            for (result in grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    isGranted = false
                    break
                }
            }
            if (isGranted) {
                lifecycleScope.launch(Dispatchers.IO) {
                    val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).canonicalPath
                    copyFileToPhone(savePath = path) {
                        skipToMainActivity()
                    }
                }
            } else {
                showToast("Permission Denied") // Permission Denied
                finish()
            }
        }
    }
}